package bist.believeinstep.impl;

import java.util.List;


import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import bist.believeinstep.dao.ConnectionDAO;
import bist.believeinstep.dao.UserDAO;
import bist.believeinstep.domain.User;
import bist.believeinstep.mapper.UserMapper;

public class UserTemplate implements UserDAO, ConnectionDAO{
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public void setDataSource(DataSource dataSource){
		
		jdbcTemplate = new JdbcTemplate(dataSource);
	}
	@Override
	public void createUser(User user){
		String query = "INSERT into bist_users (code, name, dob, gender, identify_code, identify_date, identify_place,"
				+ "email, address, phone, cellphone, role, state, username, password, pseudonym, date_create, date_modified,"
				+ "create_by, modified_by) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		try{
			jdbcTemplate.update(query,new Object[]{
				user.getCode(),
				user.getName(),
				user.getDob(),
				user.getGender(),
				user.getIdentifyCode(),
				user.getIdentifyDate(),
				user.getIdentifyPlace(),
				user.getEmail(),
				user.getAddress(),
				user.getPhone(),
				user.getCellphone(),
				user.getRole(),
				user.getState(),
				user.getUsername(),
				user.getPassword(),
				user.getPseudonym(),
				user.getCreated(),
				user.getModified(),
				user.getCreatedBy(),
				user.getModifiedBy()
			});
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	// implement update from userDAO
	@Override
	public void updateUser(User user){
		String query = "UPDATE bist_users"
				+ "SET code = ?"
				+ "name = ?"
				+ "dob = ?"
				+ "gender = ?"
				+ "identify_code = ?"
				+ "identify_date = ?"
				+ "identify_place = ?"
				+ "email = ?"
				+ "address = ?"
				+ "phone = ?"
				+ "cellphone = ?"
				+ "role = ?"
				+ "state = ?"
				+ "username = ?"
				+ "password = ?"
				+ "pseudonym = ?"
				+ "created_date = ?"
				+ "modified_date = ?"
				+ "created_by = ?"
				+ "modified_by = ?";
		try{
			jdbcTemplate.update(query,new Object[]{
				user.getCode(),
				user.getName(),
				user.getDob(),
				user.getGender(),
				user.getIdentifyCode(),
				user.getIdentifyDate(),
				user.getIdentifyPlace(),
				user.getEmail(),
				user.getAddress(),
				user.getPhone(),
				user.getCellphone(),
				user.getRole(),
				user.getState(),
				user.getUsername(),
				user.getPassword(),
				user.getPseudonym(),
				user.getCreated(),
				user.getModified(),
				user.getCreatedBy(),
				user.getModifiedBy()
			});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	// implement delete from userDAO
	@Override
	public void deleteUser(Integer id){
		String query = "DELETE from bist_users WHERE id = ?";
		try{
			jdbcTemplate.update(query,new Object[]{id});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public void deleteUser(String code){
		String query = "DELETE from bist_users WHERE code = ?";
		try{
			jdbcTemplate.update(query,new Object[]{code});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public List<User>listUser(){
		String query = "SELECT * from bist_users";
		try{
			List<User> user = jdbcTemplate.query(query, new UserMapper());
			return user;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	@Override
	public User getUser(Integer id){
		String query = "SELECT * FROM bist_users WHERE id = ?";
		try{
			User user = jdbcTemplate.queryForObject(query, new Object[]{id},new UserMapper());
			return user;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	@Override
	public User getUser(String code){
		String query = "SELECT * FROM bist_users WHERE code = ?";
		try{
			User user = jdbcTemplate.queryForObject(query, new Object[]{code}, new UserMapper());
			return user;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
}
